﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTextEdit>
#include <QPainter>




void MainWindow::addMask(QPixmap& p_img)
{
    QPainter painter(&p_img);
    int fontSize = 25, spacing = 10;
    QFont font("微软雅黑", fontSize, QFont::Thin);
    font.setLetterSpacing(QFont::AbsoluteSpacing, spacing);
    painter.setFont(font);
    painter.setPen(QColor(150, 150, 150));
    painter.translate(p_img.width() / 2, -p_img.width() / 2);//调整位置
    painter.rotate(45);
    int squareEdgeSize = p_img.width() * sin(45.0) + p_img.height() * sin(45.0);//对角线长度
    int hCount = squareEdgeSize / ((fontSize + spacing) * (text.size() + 1)) + 1;
    int x = squareEdgeSize / hCount + (fontSize + spacing) * 3;
    int y = x / 2;

    for (int i = 0; i < hCount; i++)
    {
        for (int j = 0; j < hCount * 2; j++)
        {
           painter.drawText(x * i, y * j,text);
        }
    }
}


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle(QStringLiteral("物体检测"));

    ui->startdetect->setEnabled(false);
    ui->stopdetect->setEnabled(false);
    ui->pausedetect->setEnabled(false);
    ui->textEditlog->document()->setMaximumBlockCount(60);
    detector_ = new detector();


    connect(this, &MainWindow::send_videofile_info, detector_, &detector::recv_videofile);
    connect(this, &MainWindow::send_comboBox_info, detector_, &detector::recv_comboBox_arg);

    connect(detector_, &detector::send_init_info, this, [=](QString init_info){
        ui->textEditlog->append(init_info);
        });

    connect(detector_, &detector::send_detect_info, this, [=](QString detect_info){
        ui->textEditlog->append(detect_info);
        });

    connect(detector_, &detector::send_video_info, this, [=](QString video_info){
        ui->textEditlog->append(video_info);
        });


    connect(detector_, &detector::send_display_info, this, [=](QImage rawImage){
        if (!rawImage.isNull()){
            QPixmap qpm = QPixmap::fromImage(rawImage);
            addMask(qpm);
            ui->yolo_detect->setPixmap(qpm);
        }
        });

    connect(detector_, &detector::send_comboBox_info, this, [=](QString comboBox_info){
        ui->textEditlog->append(comboBox_info);
        });

    connect(detector_, &detector::send_ready, this, [=](bool is_detect_ok){
        IsDetect_ok = is_detect_ok;
        if(IsDetect_ok){
            ui->startdetect->setEnabled(true);
            ui->stopdetect->setEnabled(false);
            ui->pausedetect->setEnabled(false);
        }
        });
}


MainWindow::~MainWindow()
{
    delete ui;
    detector_->requestInterruption();
    detector_->wait();
    delete detector_;
}


void MainWindow::on_openfile_clicked()
{
    filename = QFileDialog::getOpenFileName(this,QStringLiteral("打开文件"),".","*.mp4 *.avi");
    if(!QFile::exists(filename)){
        return;
    }
    ui->statusbar->showMessage(filename);
    QMimeDatabase db;
    QMimeType mime = db.mimeTypeForFile(filename);
    if (mime.name().startsWith("video/")) {
        emit send_videofile_info(filename);
    }else
        ui->textEditlog->append(QStringLiteral("只支持视频文件打开"));
}



void MainWindow::on_startdetect_clicked()
{
    ui->startdetect->setEnabled(false);
    ui->stopdetect->setEnabled(true);
    ui->pausedetect->setEnabled(true);
    ui->openfile->setEnabled(false);
    ui->comboBox->setEnabled(false);
    detector_->start(QThread::NormalPriority);
    ui->textEditlog->append(QStringLiteral("=======================\n"
                                           "        开始检测\n"
                                           "=======================\n"));
}

void MainWindow::on_stopdetect_clicked()
{
    ui->startdetect->setEnabled(true);
    ui->stopdetect->setEnabled(false);
    ui->pausedetect->setEnabled(false);
    ui->openfile->setEnabled(true);
    ui->comboBox->setEnabled(true);
    detector_->requestInterruption();
    ui->textEditlog->append(QStringLiteral("======================\n"
                                           "        停止检测\n"
                                           "======================\n"));
}


void MainWindow::on_comboBox_activated(const QString &arg1)
{
    emit send_comboBox_info(arg1);
}


void MainWindow::on_pausedetect_clicked()
{
    ui->startdetect->setEnabled(true);
    ui->pausedetect->setEnabled(false);
    ui->stopdetect->setEnabled(false);
    detector_->wait(30);
    detector_->requestInterruption();
}

